/**
 * Created by zhangxiaoyu on 16/4/11.
 */
angular.module('hyt.clcxService',[])
    //获取车牌号
    .factory('CphFactory',function($rootScope,ENV,$resource,Storage,$ionicLoading,$state){
        var APIUrl= Storage.get('fwqdz'),cphList={},pagesize=10;
        var resource=$resource(APIUrl);
        return{
            queryCph:function(dw_id,sid,user_id,yh_id,zd_id,cph,xl,clyxzt){
                var hasNextPage =true;
                return resource.save({
                    'ACTION_NAME' : 'sqlKeyBiz.page#cph_list_query',
                    'SID' : sid,
                    'USER_ID':user_id,
                    'ACTION_INFO':{
                        'pagesize':pagesize,
                        'current_page':'1',
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'bm_id':zd_id,
                        'cph':cph,
                        'xl':xl,
                        'clyxzt':clyxzt
                    }
                },function(r){
                    if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                        hasNextPage = false;
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                    cphList[pagesize] = {
                        hasNextPage : hasNextPage,
                        'current_page' : '2',
                        'data' : r.ACTION_INFO.RESULT_LIST
                    };
                    $rootScope.$broadcast('List.cphList');
                })
            },
            getCph:function(){
                if( cphList[pagesize] == undefined ){
                    return false;
                }

                return cphList[pagesize];
            },
            getMoreCph:function(dw_id,sid,user_id,yh_id,zd_id,cph,xl,clyxzt){
                if(cphList[pagesize] == undefined){
                    return false;
                }
                //获取以前的数据
                var hasNextPage = cphList[pagesize].hasNextPage;
                var current_page = cphList[pagesize].current_page;
                var moreCphData = cphList[pagesize].data;
                resource.save({
                    'ACTION_NAME' : 'sqlKeyBiz.page#cph_list_query',
                    'SID' : sid,
                    'USER_ID':user_id,
                    'ACTION_INFO':{
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'bm_id':zd_id,
                        'cph':cph,
                        'xl':xl,
                        'clyxzt':clyxzt
                    }
                },function(r){
                    current_page++;
                    if(r.ACTION_RETURN_CODE == "000000") {
                        if (r.ACTION_INFO.RESULT_LIST.length < pagesize) {
                            hasNextPage = false;
                        }

                        moreCphData = moreCphData.concat(r.ACTION_INFO.RESULT_LIST);

                        cphList[pagesize] = {
                            hasNextPage: hasNextPage,
                            'current_page': current_page,
                            'data': moreCphData
                        };

                        $rootScope.$broadcast('List.cphList');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            hasNextPage: function() {
                if (cphList[pagesize] === undefined) {
                    return false;
                }
                return cphList[pagesize].hasNextPage;
            }
        }
    })
    //获取装车单列表
    .factory('ZcdListFactory',function($resource, $rootScope,ENV,Storage,$ionicLoading,$state){
        var APIUrl = Storage.get('fwqdz'),
            zcdListData={},pagesize = 20;

        var resource = $resource(APIUrl);

        return {
            getZcdList: function(sid,user_id,yh_id,cl_id,cph,dw_id,zd_id) {
                var hasNextPage =true;
                return resource.save({
                    'ACTION_NAME': 'sqlKeyBiz.page#daozhan_qianshou_query',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO': {
                        'pagesize':pagesize,
                        'current_page':'1',
                        'yh_id':yh_id,
                        'cl_id':cl_id,
                        'cph':cph,
                        'zt':1,
                        'dw_id':dw_id,
                        'zd_id':zd_id
                    }
                }, function(r) {
                    if(r.ACTION_RETURN_CODE == "000000") {
                        if (r.ACTION_INFO.RESULT_LIST.length < pagesize) {
                            hasNextPage = false;
                        }

                        zcdListData[pagesize] = {
                            hasNextPage: hasNextPage,
                            'current_page': 2,
                            'data': r.ACTION_INFO.RESULT_LIST
                        };

                        $rootScope.$broadcast('zcdList.zcdUpdated');

                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });

            },
            getZcd : function(){
                if(zcdListData[pagesize] == undefined){
                    return false;
                }
                return zcdListData[pagesize];
            }
        };
    })
//获取承运单列表数据
.factory('ClcxListFactory',function($resource, $rootScope,ENV,Storage,$ionicLoading,$state){
    var APIUrl =  Storage.get('fwqdz'),
        clcxListData = {},
        pagesize = 10;

    var resource = $resource(APIUrl);

    return {
        //获取承运单列表数据
        getClcxList: function(zcd_id,cph,dw_id,sid,user_id,yh_id) {
            var hasNextPage =true;    //判断是否有下一页

            return resource.save({
                'ACTION_NAME': 'sqlKeyBiz.page#cheliangguanli',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'pagesize':pagesize,
                    'current_page':'1',
                    'zcd_id':zcd_id,
                    'dw_id':dw_id,
                    'yh_id':yh_id,
                    'cph':cph
                }
            }, function(r) {
                if(r.ACTION_RETURN_CODE == "000000") {
                    if (r.ACTION_INFO.RESULT_LIST.length < pagesize) {
                        hasNextPage = false;
                    }

                    clcxListData[pagesize] = {
                        hasNextPage: hasNextPage,
                        'current_page': 2,
                        'data': r.ACTION_INFO.RESULT_LIST
                    };

                    $rootScope.$broadcast('ClcxList.clcxUpdated');

                }else if (r.ACTION_RETURN_CODE == "000005"){
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "您的账号已在其他设备登录",
                        duration: 1500
                    });
                    $state.go("login");
                }
            });

        },

        //获取承运单列表单页数据
        getClcx : function(){
            if(clcxListData[pagesize] == undefined){
                return false;
            }
            return clcxListData[pagesize];
        },

        //获取承运单列表更多页数据
        getMoreClcx:function(zcd_id,cph,dw_id,sid,user_id,yh_id){

            //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
            if(clcxListData[pagesize] == undefined){
                return false;
            }

            //获取以前的数据
            var hasNextPage = clcxListData[pagesize].hasNextPage;
            var current_page = clcxListData[pagesize].current_page;
            var moreClcxListData = clcxListData[pagesize].data;
            resource.save({
                'ACTION_NAME': 'sqlKeyBiz.page#cheliangguanli',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'pagesize':pagesize,
                    'current_page':current_page,
                    'zcd_id':zcd_id,
                    'dw_id':dw_id,
                    'yh_id':yh_id,
                    'cph':cph
                }
            }, function (r) {

                current_page++;

                if(r.ACTION_RETURN_CODE == "000000"){
                    if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                        hasNextPage = false;
                    }

                    moreClcxListData = moreClcxListData.concat(r.ACTION_INFO.RESULT_LIST);

                    clcxListData[pagesize] = {
                        hasNextPage : hasNextPage,
                        'current_page' : current_page,
                        'data' : moreClcxListData
                    };

                    $rootScope.$broadcast('ClcxList.clcxUpdated');
                }else if (r.ACTION_RETURN_CODE == "000005"){
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "您的账号已在其他设备登录",
                        duration: 1500
                    });
                    $state.go("login");
                }


            })
        },

        hasNextPage: function() {
            if (clcxListData[pagesize] === undefined) {
                return false;
            }
            return clcxListData[pagesize].hasNextPage;
        }
    }
})
//车辆实时定位及静态数据
    .factory('GPSLocationFactory',function($resource, $rootScope,Storage){

        var APIUrl = "http://192.168.5.251:44879/api/CarInfo/Info",
            result;
        var resource = $resource(APIUrl,{},{
            query :{
                method : 'get',
                params : {
                    "phoneNum": "013000000000",
                    "key":"b4e158c6-78da-4a2f-9850-59ff3a19032c"
                },
                tiemout : 2000
            }
        });
        return{
            //获取数据
            getGPS : function(){
                resource.query({
                },function(r){
                    result=r;
                    console.log(result);
                    $rootScope.$broadcast('GPS.locationUpdated');
                })
            },
            //返回我们保存的数据
            getResult:function(){
                return result;
            }
        };
        //var resource = $resource(APIUrl);
        //
        //return {
        //    getGPS: function() {
        //        return resource.save({
        //            "phoneNum": "013000000000",
        //            "key":"b4e158c6-78da-4a2f-9850-59ff3a19032c"
        //        }, function(r) {
        //            result= r;
        //            console.log(result);
        //            $rootScope.$broadcast('GPS.locationUpdated');
        //        });
        //
        //    },
        //    getResult : function(){
        //        return result;
        //    }
        //};
    })
    //车辆轨迹
    .factory('TrailFactory',function($resource, $rootScope,Storage){

        var APIUrl = "http://192.168.5.251:44879/api/CarInfo/InfoData",
            result;
        var resource = $resource(APIUrl);

        return {
            getTrail: function() {
                return resource.save({
                    "PhoneNum": "13122301001",
                    "Key":"b4e158c6-78da-4a2f-9850-59ff3a19032c",
                    "StarTime":"2015-01-02 08:07:16",
                    "EndTime":"2015-01-02 21:01:01",
                    "IntervalTime":600
                }, function(r) {
                    result= r;
                    console.log(result);
                    $rootScope.$broadcast('Trail.trailUpdated');
                });

            },
            getResult : function(){
                return result;
            }
        };
    })
    //获取经纬度
    .factory('ClGPSFactory',function($resource,$rootScope,ENV,Storage){
        var api =  Storage.get('fwqdz'),
            GPSLocation;

        var resource = $resource(api);

        return{
            getGPS : function(cl_id,sid,user_id,yh_id,dw_id){
                return resource.save({
                    "ACTION_NAME" : "sqlKeyBiz.fetch#location_query",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        'state':2,
                        "cl_id" : cl_id,
                        'dw_id':dw_id
                    }
                },function(r){
                    console.log(r);
                    GPSLocation = r.ACTION_INFO;
                    $rootScope.$broadcast('ClGPS.GPSUpdated');
                });
            },

            getResult : function(){
                return GPSLocation;
            }
        }
    });
